(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives
	     '("melpa" . "https://melpa.org/packages/"))

(package-initialize)

(add-to-list 'load-path "~/.emacs.d/elisp/")

(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))

;; PACKAGE INSTALLS
(use-package which-key
  :ensure t
  :init
  (which-key-mode))

(add-hook 'text-mode-hook 'turn-on-auto-fill)

(use-package xah-fly-keys
  :ensure t)
(require 'xah-fly-keys)
(xah-fly-keys-set-layout "qwerty") ; required
(xah-fly-keys 1)

(use-package moe-theme
  :ensure t)

(require 'moe-dark-theme)
;; (load-theme 'moe-dark t)
(setq moe-dark-enabled 1)

(use-package beacon
  :ensure t
  :init
  (beacon-mode 1))

(use-package clips-mode
  :ensure t)

(use-package go-mode
  :ensure t)

(use-package expand-region
  :ensure t)

(use-package glsl-mode
  :ensure t)

(use-package bison-mode
  :ensure t)


;; Org-mode
(require 'ox-latex)
(unless (boundp 'org-latex-classes)
  (setq org-latex-classes nil))
;; (add-to-list 'org-latex-classes
             ;; '("article"
               ;; "\\documentclass{article}"
               ;; ("\\section{%s}" . "\\section*{%s}")
               ;; ("\\subsection{%s}" . "\\subsection*{%s}")
               ;; ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
               ;; ("\\paragraph{%s}" . "\\paragraph*{%s}")
               ;; ("\\subparagraph{%s}" . "\\subparagraph*{%s}")))




(load "hl-defined.el")
(require 'hl-defined)
(add-hook 'emacs-lisp-mode-hook 'hdefd-highlight-mode 'APPEND)

;;;;; Preferences

;; Xah Fly Keys
(global-set-key (kbd "C-c") 'xah-fly-command-mode-activate)

; SPC-i-n
(defun open-init-file () (interactive) (find-file "~/.emacs.d/init.el"))
(define-key xah-fly-c-keymap (kbd "n") 'open-init-file)

; SPC-i-v
(defun eval-elisp-buffer () (interactive) (save-buffer) (eval-buffer))
(define-key xah-fly-c-keymap (kbd "v") 'eval-elisp-buffer)

; SPC-w-s
(defun select-inner-word () (interactive) (er/mark-word))
(define-key xah-fly-comma-keymap (kbd "s") 'select-inner-word)

; 9 -> er/expand region
(defun xah-select-text-in-quote ()
  (interactive)
  (call-interactively 'er/expand-region)
  )

; Theme
(defun change-moe-theme () (interactive) (if moe-dark-enabled (progn (load-theme 'moe-light t) (setq moe-dark-enabled nil)) (progn (load-theme 'moe-dark t) (setq moe-dark-enabled 1))))
(define-key xah-fly-w-keymap (kbd "t") 'change-moe-theme)

; Scroll
(define-key xah-fly-dot-keymap (kbd "i") 'scroll-down-command)
(define-key xah-fly-dot-keymap (kbd "k") 'scroll-up-command)
(setq scroll-conservatively 100)
(global-set-key (kbd "C-k") 'scroll-up-line)
(define-key input-decode-map (kbd "C-i") (kbd "H-i"))
(global-set-key (kbd "H-i") 'scroll-down-line)

;; Miscelaneus
(tool-bar-mode -1) ; Copiar, pegar, abrir, undo...
(menu-bar-mode -1) ; Archivo, Editar...
(scroll-bar-mode -1)
(setq ring-bell-function 'ignore)
(set-face-attribute 'default nil :height 120)

;; Tabs
(setq custom-tab-width 2)
(setq-default python-indent-offset custom-tab-width)
(setq-default tab-width 4)

;; Highlight line
(when window-system (global-hl-line-mode t))
;; (set-face-background 'hl-line "gray15")
(show-paren-mode 1)
(setq show-paren-style 'expression)

;; Prettify symbols
(when window-system (global-prettify-symbols-mode t))

;; Syntax highlighting
(font-lock-add-keywords
 'c-mode 'glsl-mode
 '(("\\<\\(\\sw+\\) ?(" 1 'font-lock-function-name-face)))

(font-lock-add-keywords
 'clips-mode
 '(
   ("$?\\?\\([a-zA-Z0-9]+\\(-[a-zA-Z0-9]\\)?\\)+"        0 'font-lock-variable-name-face)   ;variables
   ("\\?\\*\\([a-zA-Z0-9]+\\(-[a-zA-Z0-9]\\)?\\)+\\*" 0 'font-lock-type-face) ; global variables
   )
 )

(font-lock-add-keywords 'c++-mode
 `((,(concat
   "\\<[_a-zA-Z][_a-zA-Z0-9]*\\>"       ; Object identifier
   "\\s *"                              ; Optional white space
   "\\(?:\\.\\|->\\)"                   ; Member access
   "\\s *"                              ; Optional white space
   "\\<\\([_a-zA-Z][_a-zA-Z0-9]*\\)\\>" ; Member identifier
   "\\s *"                              ; Optional white space
   "(")                                 ; Paren for method invocation
   1 'font-lock-function-name-face t)))	
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(package-selected-packages
   (quote
	(bison-mode glsl-mode expand-region go-mode clips-mode beacon moe-theme xah-fly-keys which-key use-package))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )
